import router from "./index.js"
import store from "@/store/index"
import { getToken } from "@/utils"
router.beforeEach((to, from, next) => {
    if(!getToken()) {
        if(to.name === 'login') {
            next()
        }else{
            next('/login'); // 1、路由转向；2、重新进入router.beforeEach
        }
    }else{
        if(store.state.permission.asyncRouter.length === 0) {
            store.dispatch("permission/activeGetAuth").then(res => {
                // 获取动态路由
                const async_router_data = store.state.permission.asyncRouter
                // 获取静态路由
                const default_router_data = router.options.routes;
                // 更新静态路由
                router.options.routes = default_router_data.concat(async_router_data);
                // 更新动态路由
                async_router_data.forEach(item => {
                    router.addRoute(item);
                });
                // 确认进入下一个路由
                next({ ...to, replace: true});
            })
        }else{
            next()
        }
    }   
});

router.afterEach(() => {
// NProgress.done();
});
